#include <glib.h>

typedef struct mydata_tag {
    int id;
    char name[32];
}my_data_t;

void my_printf(gpointer data, gpointer user_data)
{
    g_print("my_printf: id=%d, name=%s, user_data=%s\n", ((my_data_t *)data)->id, ((my_data_t *)data)->name, (char *)user_data);

}

static void test_slist_append_struct(void)
{
    int i;
    GSList *slist = NULL;
    GSList *s;
    my_data_t mydata[16];

    memset(mydata, 0, sizeof(my_data_t)*5);
    for(i=0;i<5;i++) {
        mydata[i].id = i;
        g_snprintf(mydata[i].name, 16, "data is %d", i);
        slist = g_slist_append(slist, (gpointer)&mydata[i]);
    }

    g_print("\n");

    s = slist;
    while (NULL != s) {
        g_printf("the slist data: %d, %s \n", ((my_data_t *)(s->data))->id, ((my_data_t *)(s->data))->name);
        s = s->next;
    }

    g_slist_foreach(slist, my_printf, "user_data_str");

    g_slist_free(slist);
}

gint main (gint argc, gchar *argv[])
{
    gint i;

    g_test_init (&argc, &argv, NULL);

    g_test_add_func ("/slist/append", test_slist_append_struct);

    return g_test_run ();
}
